package com.example.myapplication.tack_15.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_15.data.CommentData;
import com.example.myapplication.utils.OkHttpTool;

import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.My> {
    private List<CommentData.RowsDTO> list;
    private Context context;
    private View view;

    public CommentAdapter(List<CommentData.RowsDTO> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_15_comment,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        Glide.with(context).load(OkHttpTool.URL + list.get(position).getFromUserAvatar()).into(holder.fromUserAvatar);
        holder.userName.setText(list.get(position).getFromUserName());
        holder.evaluateContent.setText(list.get(position).getEvaluateContent());
        holder.likeCount.setText("点赞数："+list.get(position).getLikeCount());
        if(list.get(position).isMyLikeState()){
            holder.myLikeState.setText("已点赞");
        }else{
            holder.myLikeState.setText("未点赞");
        }
        holder.myLikeState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.myLikeState.setText("已点赞");
                list.get(position).setMyLikeState(true);
                list.get(position).setLikeCount(list.get(position).getLikeCount()+1);
            }
        });


    }

    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView fromUserAvatar;
        private TextView userName;
        private TextView evaluateContent;
        private TextView likeCount;
        private TextView myLikeState;
        public My(@NonNull View itemView) {
            super(itemView);
            fromUserAvatar = view.findViewById(R.id.fromUserAvatar);
            userName = view.findViewById(R.id.userName);
            evaluateContent = view.findViewById(R.id.evaluateContent);
            likeCount = view.findViewById(R.id.likeCount);
            myLikeState = view.findViewById(R.id.myLikeState);

        }
    }
}
